package com.retoc.oawx.controller;

import cn.hutool.core.util.IdUtil;
import com.retoc.oawx.common.util.R;
import com.retoc.oawx.config.shiro.JWTUtil;
import com.retoc.oawx.domain.Leave;
import com.retoc.oawx.domain.req.CreateLeaveReq;
import com.retoc.oawx.domain.req.DeleteReq;
import com.retoc.oawx.domain.req.PageReq;
import com.retoc.oawx.service.LeaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/leave")
@Api("请假模块接口")
@Slf4j
public class LeaveController {
    @Autowired
    private JWTUtil jwtUtil;

    @Autowired
    private LeaveService leaveService;

    @PostMapping("/searchMyLeaveListByPage")
    @ApiOperation("查询请假列表分页数据")
    public R searchMyLeaveListByPage(@Valid @RequestBody PageReq req, @RequestHeader("token") String token){
        int userId = jwtUtil.getUserId(token);
        Integer page = req.getPage();
        Integer length = req.getLength();
        Boolean old = req.getOld();
        int start = (page - 1) * length;
        HashMap map = new HashMap<>();
        map.put("userId", userId);
        map.put("start", start);
        map.put("length", length);
        ArrayList<HashMap> result=new ArrayList<>();
        if (old){
            result = leaveService.selectLeaveListByCreator(map);
        }else {
            result = leaveService.selectoldLeaveListByCreator(map);
        }

        return R.ok().put("result",result);
    }

    @PostMapping("/selectLeaveById")
    @ApiOperation("根据请假单ID查询请假单信息")
    public R selectLeaveById(@Valid @RequestBody DeleteReq req){
        HashMap result = leaveService.selectLeaveById(req.getId());
        return R.ok().put("result", result);
    }

    @PostMapping("/insertLeave")
    @ApiOperation("新建请假单")
    public R insertLeave(@Valid @RequestBody CreateLeaveReq req,@RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);
//        SimpleDateFormat fmt=new SimpleDateFormat("hh:mm:ss");
        Leave leave =new Leave();
        leave.setUuid(IdUtil.simpleUUID());
        leave.setTitle(req.getTitle());
        leave.setCreatorId(userId);
        leave.setStart(req.getStart());
        leave.setEnd(req.getEnd());
        leave.setStartTime(req.getStartTime()+":00");
        leave.setEndTime(req.getEndTime()+":00");
//        leave.setStartTime(fmt.format(req.getStartTime()));
//        leave.setEndTime(fmt.format(req.getEndTime()));
        leave.setType(req.getType());
        leave.setReason(req.getReason());
        leave.setStatus(0);
        leave.setCreateTime(new Date());
//        System.out.println("leave = " + leave);
        leaveService.insertLeave(leave);
        return R.ok();
    }

    @DeleteMapping("/deleteLeaveById")
    @ApiOperation("根据请假单ID删除用车单")
    public R deleteLeaveById(@Valid @RequestBody DeleteReq req){
        int i = leaveService.deleteByid(req.getId());
        return R.ok().put("result","已删除"+i+"条记录");
    }

}
